package www.openteam.com.ui.qualityCntrl;


import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.adapter.qualityCntrl.IPQCAdapter00;
import www.openteam.com.adapter.qualityCntrl.IPQCAdapter01;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.IPQCCheckMan;
import www.openteam.com.bean.IPQCMachine;
import www.openteam.com.bean.IPQCMsg00;
import www.openteam.com.bean.IPQCMsg01;
import www.openteam.com.bean.IPQCProduce;
import www.openteam.com.contract.qualityCntrl.IPQCContract;
import www.openteam.com.presenter.qualityCntrl.IPQCPresenter;

/**
 * A simple {@link Fragment} subclass.
 */
public class IPQCFragment extends mBaseFragment<IPQCPresenter>
implements IPQCContract.IPQCView{


    @Bind(R.id.ipqc_produce)
    TextView ipqcProduce;
    @Bind(R.id.ipqc_spinner_produce)
    Spinner ipqcSpinnerProduce;
    @Bind(R.id.ipqc_btn_produce)
    Button ipqcBtnProduce;
    @Bind(R.id.ipqc_tv_date)
    TextView ipqcTvDate;
    @Bind(R.id.ipqc_machinecode)
    TextView ipqcMachinecode;
    @Bind(R.id.ipqc_spinner_machinecode)
    Spinner ipqcSpinnerMachinecode;
    @Bind(R.id.ipqc_checkMan)
    TextView ipqcCheckMan;
    @Bind(R.id.ipqc_spinner_checkMan)
    Spinner ipqcSpinnerCheckMan;
    @Bind(R.id.ipqc_team)
    TextView ipqcTeam;
    @Bind(R.id.ipqc_spinner_team)
    Spinner ipqcSpinnerTeam;
    @Bind(R.id.ipqc_checkType)
    TextView ipqcCheckType;
    @Bind(R.id.ipqc_spinner_checkType)
    Spinner ipqcSpinnerCheckType;
    @Bind(R.id.ipqc_tv_note)
    TextView ipqcTvNote;
    @Bind(R.id.ipqc_et_note)
    EditText ipqcEtNote;
    @Bind(R.id.query_btn)
    Button queryBtn;
    @Bind(R.id.ipqc_Rcycleview)
    RecyclerView ipqcRcycleview;
    private List<IPQCProduce.DataBeanX.DataBean> produceData;
    private List<IPQCMachine.DataBeanX.DataBean> machineData;
    private List<IPQCCheckMan.DataBeanX.DataBean> checkManData;
    private IPQCAdapter01 ipqcAdapter01;
    private IPQCAdapter00 ipqcAdapter00;
    private String TAG = this.getClass().getSimpleName();
    JumpData jumpData;


    public IPQCFragment() {
        // Required empty public constructor
    }

    public void setJumpData(JumpData jumpData) {
        this.jumpData = jumpData;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_ipqc, container, false);
        ButterKnife.bind(this, view);
        mPresenter = loadPresenter();
        initData();
        initView();
        initListener();
        return view;
    }

    @Override
    protected void initData() {
        if (mPresenter != null)
            mPresenter.attachView(this);

        ArrayList<String> teamList = new ArrayList<>();
        teamList.add("日班");
        teamList.add("夜班");
        ArrayAdapter<String> teamAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, teamList);
        teamAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ipqcSpinnerTeam.setAdapter(teamAdapter);

        ArrayList<String> checkTypeList = new ArrayList<>();
        checkTypeList.add("生产工序IPQC检验");
        checkTypeList.add("发外收货IPQC检验");
        ArrayAdapter<String> CheckTypeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, checkTypeList);
        CheckTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ipqcSpinnerCheckType.setAdapter(CheckTypeAdapter);
        //设置检验人员
        mPresenter.findCheckMan();
    }

    @Override
    protected void initListener() {
        //检验日期
        ipqcTvDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ipqcTvDate.setTag(ipqcTvDate);
                selectCalendar(ipqcTvDate);
            }
        });

        //点击确定开始查询
        queryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null!=produceData&&0!=ipqcSpinnerCheckType.getChildCount()){
                    String code = produceData.get(ipqcSpinnerProduce.getSelectedItemPosition()).getCode();
                    if (ipqcSpinnerCheckType.getSelectedItemPosition()==0){//00
                        createProgressDialog("正在查询");
                        mPresenter.findMsg00(code);
                    }else if (ipqcSpinnerCheckType.getSelectedItemPosition()==1){//01
                        createProgressDialog("正在查询");
                        mPresenter.findMsg01(code);
                    }
                }else {
                    createProgressDialog("工序和检验类型不能为空!");

                }
            }
        });
    }

    @Override
    protected void initView() {
        ipqcRcycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        ipqcAdapter00 = new IPQCAdapter00(getContext());
        ipqcAdapter01 = new IPQCAdapter01(getContext());
        //点击工序后的查询按钮
        ipqcBtnProduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createProgressDialog("正在查询");
                mPresenter.findProduce();
            }
        });
        //工序列表选择事件
        ipqcSpinnerProduce.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (null!=produceData){
                    //得到工序编码
                    String code = produceData.get(position).getCode();
                    mPresenter.findProMachine(code);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }

    public void createProgressDialog(String content) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getContext());
        }
        mProgressDialog.setMessage(content);
        mProgressDialog.show();


    }



    public void selectCalendar(final TextView v){
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                v.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
            }
        }, calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected IPQCPresenter loadPresenter() {
        return new IPQCPresenter();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void findProduce(IPQCProduce IPQCProduce) {
        mProgressDialog.dismiss();
        ArrayList<String> produceList = new ArrayList<>();
        produceData = IPQCProduce.getData().getData();
        for (IPQCProduce.DataBeanX.DataBean dataBean:produceData) {
            produceList.add(dataBean.getName());
        }
        ArrayAdapter<String> produceAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, produceList);
        produceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ipqcSpinnerProduce.setAdapter(produceAdapter);
    }

    @Override
    public void findProMachine(IPQCMachine IPQCMachine) {
        ArrayList<String> machineList = new ArrayList<>();
        machineData = IPQCMachine.getData().getData();
        for (IPQCMachine.DataBeanX.DataBean dataBean:machineData) {
            machineList.add(dataBean.getName());
        }
        ArrayAdapter<String> machineAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, machineList);
        machineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ipqcSpinnerMachinecode.setAdapter(machineAdapter);
    }

    @Override
    public void findCheckMan(IPQCCheckMan ipqcCheckMan) {
        ArrayList<String> checkManList = new ArrayList<>();
        checkManData = ipqcCheckMan.getData().getData();
        for (IPQCCheckMan.DataBeanX.DataBean dataBean:checkManData) {
            checkManList.add(dataBean.getName());
        }
        ArrayAdapter<String> checkManAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, checkManList);
        checkManAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ipqcSpinnerCheckMan.setAdapter(checkManAdapter);
    }

    @Override
    public void findMsg00(IPQCMsg00 ipqcMsg00) {
        mProgressDialog.dismiss();
        if (null!=ipqcMsg00.getData()){
            final List<IPQCMsg00.DataBeanX.DataBean> data = ipqcMsg00.getData().getData();
            ipqcAdapter00.setData00(data);
            ipqcRcycleview.setAdapter(ipqcAdapter00);
            ipqcAdapter00.setOnLongClickListener(new IPQCAdapter00.OnLongClickListener() {
                @Override
                public void onLongClick(int postion) {
                    if (jumpData!=null){
                        Log.e(TAG, "onLongClick: "+postion );
                        jumpData.onclickListener00(data.get(postion));
                    }
                }
            });
        }else {
                Toast.makeText(getContext(), ipqcMsg00.getInfo(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void findMsg01(IPQCMsg01 ipqcMsg01) {
        mProgressDialog.dismiss();
        if (null!=ipqcMsg01.getData()){
            final List<IPQCMsg01.DataBeanX.DataBean> data = ipqcMsg01.getData().getData();
            ipqcAdapter01.setData01(data);
            ipqcRcycleview.setAdapter(ipqcAdapter01);
            ipqcAdapter01.setOnLongClickListener01(new IPQCAdapter01.OnLongClickListener() {
                @Override
                public void onLongClick(int postion) {
                    if (jumpData!=null){
                        Log.e(TAG, "onLongClick: "+postion );
                        jumpData.onclickListener01(data.get(postion));
                    }
                }
            });
        }else {
                Toast.makeText(getContext(), ipqcMsg01.getInfo(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void fail(String msg) {
        Log.e(TAG, "fail: ||"+msg );
    }


    public interface JumpData{

        void onclickListener00(IPQCMsg00.DataBeanX.DataBean dataBean);

        void onclickListener01(IPQCMsg01.DataBeanX.DataBean dataBean);
    }

}
